package xjtu.sad.zbsj.user.register.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xjtu.sad.zbsj.user.register.RegisterDto;
import xjtu.sad.zbsj.user.register.RegisterService;
import xjtu.sad.zbsj.user.register.dao.RegisterDao;

import javax.annotation.Resource;

/**
 * @author lkm
 */
@Slf4j
@Service
@Transactional
public class RegisterServiceImpl implements RegisterService {

    @Resource
    private RegisterDao registerDao;

    @Override
    public void addUser(RegisterDto dto) {
//        Calendar cal =Calendar.getInstance();
//        int year = cal.get(Calendar.YEAR);
//        int month = cal.get(Calendar.MONTH) + 1;
//        String mon = month<10 ? ("0"+month):(""+month);
//        int day = cal.get(Calendar.DAY_OF_MONTH);
//        String d = day<10 ? ("0"+day):(""+day);
//        String prefix= "U" + year + mon + d;
//        String newNum = registerDao.findUserNewNum(prefix);
//        dto.setU_id(prefix+newNum);
        String u_id = registerDao.getNewU_Id();
        dto.setU_id(u_id);
        registerDao.addUser(dto);
        registerDao.addToCredit(u_id, dto.getU_name(), 0);
    }

    @Override
    public boolean checkCount(String name) {
        int cnt = registerDao.checkCount(name);
        return cnt == 0;
    }

    @Override
    public boolean checkCount2(String email) {
        int cnt = registerDao.findEmlCount(email);
        return cnt == 0;
    }
}
